package com.bsj.sipgateway.sip.session;

import com.bsj.sipgateway.cache.ExpiredHandler.impl.CommandExpiredHandler;
import com.bsj.sipgateway.cache.ExpiredTimedCache;
import com.bsj.sipgateway.sip.bean.Command;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class CommandManager {

    //保存uuid，用于响应mq
    private static ExpiredTimedCache<String, Command> commandCache = new ExpiredTimedCache<>(30000, new CommandExpiredHandler());

    @PostConstruct
    public void init() {
        commandCache.schedulePrune(1000);
    }

    //key：deviceId_branch    branch：发送设备参数
    public static void put(String deviceId,String branch, String uuid, int cmdType,String channel,String deviceIdReal,String videoType) {
        commandCache.put(deviceId+"_"+branch, new Command(uuid, cmdType,videoType,channel,deviceIdReal));
    }

    public static void putWithSessionType(String deviceId,String branch, String uuid, int cmdType, String sessionType,String channel) {
        commandCache.put(deviceId+"_"+branch, new Command(uuid, cmdType, sessionType,channel,deviceId));
    }

    //不刷新过期时间
    public static Command get(String deviceId,String branch){
        return commandCache.get(deviceId+"_"+branch, false);
    }

    public static void remove(String deviceId,String branch) {
        commandCache.remove(deviceId+"_"+branch);
    }

    public static void remove(String key) {
        commandCache.remove(key);
    }

    //语音广播时存放uuid，key：deviceId_broadcast
    public static void putWithBroadcast(String deviceId, String uuid, int cmdType,String channel,String deviceIdReal){
        commandCache.put(deviceId+"_"+"broadcast", new Command(uuid, cmdType,null,channel,deviceIdReal));
    }

    //刷新过期时间
    public static Command getWithBroadcast(String deviceId){
        return commandCache.get(deviceId+"_"+"broadcast", true);
    }

    public static void removeWithBroadcast(String deviceId) {
        commandCache.remove(deviceId+"_"+"broadcast");
    }

}
